package com.pipayshop.controller;

import com.pipayshop.domain.dto.StoreCommodityEvaluateDTO;
import com.pipayshop.domain.vo.PageDataVO;
import com.pipayshop.service.PhysicalStoreCommodityEvaluateService;
import com.pipayshop.entity.vo.ResponseVO;
import com.pipayshop.exception.BusinessException;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import jakarta.annotation.Resource;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author zxb
 * @since 2023-07-27
 */
@RestController
@Api(value = "实体店商品评价表", tags = "实体店商品评价表")
@RequestMapping("/pipayshopapi/store-commodity-evaluate")
public class PhysicalStoreCommodityEvaluateController {

    @Resource
    private PhysicalStoreCommodityEvaluateService evaluationService;

    @GetMapping("commodityEvaluateList/{commodityId}/{pageNum}/{pageSize}")
    @ApiOperation("实体店-商品-评论列表")
    @Cacheable(value = "commodityEvaluateList", key = "'commodityEvaluateList'+#commodityId")
    public ResponseVO<PageDataVO> commodityEvaluateList(@PathVariable String commodityId,
                                                        @ApiParam("当前页")
                                                        @PathVariable Integer pageNum,
                                                        @ApiParam("展示条数")
                                                        @PathVariable Integer pageSize) {
        try {
            PageDataVO result = evaluationService.commodityEvaluateList(commodityId, pageNum, pageSize);
            return ResponseVO.getSuccessResponseVo(result);
        } catch (Exception e) {
            throw new BusinessException("view_comment_list_failed");
        }
    }

    @PostMapping("addEvaluate")
    @ApiOperation("实体店-商品-添加评论")
    @CacheEvict(value = "commodityEvaluateList",allEntries = true)
    public ResponseVO<String> addEvaluate(StoreCommodityEvaluateDTO dto) {
        try {
            Boolean result = evaluationService.addEvaluate(dto);
            if (!result) {
                return ResponseVO.getFalseResponseVo("新增评论失败");
            }
            return ResponseVO.getSuccessResponseVo("新增评论成功");
        } catch (BusinessException e) {
            throw new BusinessException(e.getMessage());
        }
        catch (Exception e) {
            throw new BusinessException("publish_comment_failed");
        }
    }

    @PostMapping("deleteEvaluate/{evaluateId}/{userId}")
    @ApiOperation("实体店-商品-删除评论")
    @CacheEvict(value = "commodityEvaluateList", allEntries = true)
    public ResponseVO<String> deleteEvaluate(@PathVariable String evaluateId, @PathVariable String userId) {
        try {
            Boolean result = evaluationService.deleteEvaluate(evaluateId, userId);
            if (!result) {
                return ResponseVO.getFalseResponseVo("delete_comment_failed");
            }
            return ResponseVO.getSuccessResponseVo("delete_success");
        } catch (Exception e) {
            throw new BusinessException("delete_comment_failed");
        }
    }
}
